package com.example.wc.logindemolistview;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import static android.R.color.black;
import static com.example.wc.logindemolistview.LoginDemoActivity.ACCESS;

public class WriteActivity extends AppCompatActivity {

    public static final String EXTRA_BLOG_ID = "com.example.wc.logindemo.blog_id";

    private TextView mBack;
    private EditText mName;
    private EditText mContent;
    private Button mWrite;

    private Blogs mBlogs;

    //传入1/3:newIntent()方法附加extra信息blogId
    public static Intent newIntent(Context packageContext, String blogId){
        Intent intent = new Intent(packageContext, WriteActivity.class);
        intent.putExtra(EXTRA_BLOG_ID, blogId);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write);

        mBack = (TextView) findViewById(R.id.back_title_write_activity);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WriteActivity.this, HomepageActivity.class);
                startActivity(intent);
            }
        });

        mName = (EditText)findViewById(R.id.name_editText);
        mContent =(EditText)findViewById(R.id.content_editText);
        mWrite = (Button)findViewById(R.id.write_button);
        mWrite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject jsonObject = new JSONObject();

                try {
//                    jsonObject.put("name", "江城子");
//                    jsonObject.put("content", "十年生死两茫茫，不思量，自难忘。");
                    jsonObject.put("name", mName.getText());
                    jsonObject.put("content", mContent.getText());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                        "http://www.diyerland.com/diyerland/api/blogadd",
                        jsonObject,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {

                                if(response == null)
                                    return;

                                try {
                                    Integer error = (Integer) response.get("error");
                                    if(error == 0) {
//                                        JSONObject data = response.getJSONObject("data");
//                                        String name = data.getString("name");
//                                        String accessToken = data.getString("accessToken");
//                                        Toast.makeText(WriteActivity.this,accessToken,Toast.LENGTH_SHORT).show();
                                        Log.d("TAG", "发布成功");
                                        Intent i = new Intent(WriteActivity.this, HomepageActivity.class);
                                        startActivity(i);
                                    }

                                } catch (JSONException e) {
//                                    e.printStackTrace();
                                    Toast.makeText(WriteActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(WriteActivity.this,error.getMessage(),Toast.LENGTH_SHORT).show();
                            }
                        }
                ){
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError{
                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("Content-Type", "application/json");
                        headers.put("accessToken", ACCESS);
                        return headers;
                    }
                };
                Volley.newRequestQueue(WriteActivity.this).add(jsonObjectRequest);
            }
        });

        //传入3/3:获取extra信息
        String blogId = (String) getIntent().getSerializableExtra(EXTRA_BLOG_ID);
        Log.d("id", blogId);
        Log.d("accessToken", ACCESS);
        String url = "http://www.diyerland.com/diyerland/api/blogById?blogId=" + blogId;
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        Gson gson = new Gson();
                        mBlogs = gson.fromJson(response.toString(), Blogs.class);
                        Log.d("TAG", mBlogs.toString());

                        mName.setText(mBlogs.getName().toString());
                        mName.setEnabled(false);
                        mName.setTextColor(getResources().getColor(black));
                        mContent.setText(mBlogs.getContent().toString());
                        mContent.setEnabled(false);
                        mContent.setTextColor(getResources().getColor(black));
                        mWrite.setVisibility(View.GONE);
//                        //EditText自动换行
//                        //设置EditText的显示方式为多行文本输入
//                        mContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
//                        //文本显示的位置在EditText的最上方
//                        mContent.setGravity(Gravity.TOP);
//                        //改变默认的单行模式
//                        mContent.setSingleLine(false);
//                        //水平滚动设置为False
//                        mContent.setHorizontallyScrolling(false);

                        Log.d("TAG", mBlogs.toString());
                    }

                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    Log.d("error", error.toString());

                    }
                }
        ){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError{
                HashMap<String, String> headers = new HashMap<String, String>();
//                headers.put("Content-Type", "application/json");
                headers.put("accessToken", ACCESS);
                return headers;
            }
        };
        Volley.newRequestQueue(this).add(jsonObjectRequest);


    }
}
